define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {
		$scope.form = {};
		var currentNode = null;
		var menuTreeObj;
		var tId = "";

		//单击
		var zTreeOnClick = function(event, treeId, treeNode) {
			$scope.dept_name = treeNode.name;
			currentNode = treeNode;
			tId = currentNode.tId;

			$scope.findrole(treeNode);
			$scope.$apply();
		};
		// zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
		var setting = {
			data: {
				simpleData: {
					enable: true
				}
			},
			check: {
				enable: false
			},
			edit: {
				enable: true
			},
			view:{
				showIcon: false,
				selectedMulti: false
			},
			callback: {
				onClick: zTreeOnClick
			}
		};
		//学校部门
		$scope.findrolegroup = function(){
			var zNodes =  [];
			httpService.post(app.urls.role_group_findGroupTree, {}, function(data){
				if("0000" === data.code) {
					zNodes = data.data;
					menuTreeObj = $.fn.zTree.init($("#groupTree"), setting, zNodes);
					$scope.$apply();

					var nodes = menuTreeObj.getNodes();
					if (nodes.length > 0) {
						currentNode = nodes[0];
						menuTreeObj.selectNode(nodes[0]);

						menuTreeObj.expandAll(true);
						setting.callback.onClick(null, menuTreeObj.setting.treeId, nodes[0]);
					}

				}else{
					eventBusService.publish("modal.alert", {"content": data.msg});
				}
			});
		}
		$scope.findrolegroup();


		//按钮点击事件-设置应用菜单
		$scope.selectMenu = function(fk_client){
			$("#dataPk_"+fk_client).attr("checked","checked");
			$scope.form.pk_app = fk_client;
			$scope.fk_client = fk_client;
			$scope.addApp();
		}

		$scope.findrole = function(row){
			$scope.groupt = row;
			$scope.form.fk_group=row.id;
			httpService.post(app.urls.role_selectByType,$scope.form,function(data){
				if("0000" === data.code){
					$scope.roles = data.data;
					$scope.selectApp($scope.roles[0],$scope.roles[0].type);
				}else{
					alert(data.msg);
				}
			});
		}

		//查询应用数据
		$scope.selectApp = function(row,type){
			$scope.fk_roleid = row.id;
			$scope.form.fk_roleid = row.id;
			$scope.row_name=row.name;
			$scope.fk_groupid=row.fk_groupid;
			$scope.type=type;

			if(type == '0'){
				$scope.show="";
				$("#"+$scope.controllerName + "_th").addClass("fe-folder" );
				$("#"+$scope.controllerName + "_th").removeClass("fe-user");
			}else{
				$scope.show="true";
				$("#"+$scope.controllerName + "_th").addClass("fe-user" );
				$("#"+$scope.controllerName + "_th").removeClass("fe-folder");
				httpService.postpage(app.urls.app_select,$scope.form,function(data){
					if("0000" === data.code){
						$scope.rows = data.data;
						$scope.selectApps();
					}else{
						alert(data.msg);
					}
				});
			}


		}

		$scope.find = function(){
			var row = {"id":$scope.fk_roleid,"name":$scope.row_name,"fk_groupid":$scope.fk_groupid};
			$scope.selectApp(row,$scope.type);
		}


		if(params.fk_roleid != undefined ){
			$scope.form.id = params.fk_roleid;
			httpService.post(app.urls.role_selectById,$scope.form,function(data){
				if("0000" === data.code){
					$scope.role_name = data.data.name;
					var row = {'id':params.fk_roleid,'name':$scope.role_name,'fk_groupid':params.fk_groupid};
					$scope.selectApp(row,params.type);
				}else{
					alert(data.msg);
				}
			});
		}

		$scope.selectApps= function(){

			httpService.post(app.urls.app_selectRoleApp,$scope.form,function(data){
				if("0000" === data.code){
					$scope.applist = data.data;
					for(var i =0;i<$scope.applist.length;i++){
						$("#"+"dataPk_"+$scope.applist[i].client_id).prop("checked", true);
					}
				}else{
					alert(data.msg);
				}
			});
		}

		$scope.addApp = function(){
			$scope.values = [];
			$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
				$scope.values.push($(this).val());
			});
			if($scope.values.length < 1){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择修改数据。"});
			}else{
				$scope.deleteApp();
			}

		}

		$scope.saveApp = function(){
			httpService.post(app.urls.role_power_insert,{"fk_roleid":$scope.form.fk_roleid,"pk_app":$scope.form.pk_app,"type":"1"},function(data){
				if("0000" === data.code){
					var menu = {"url":"modules/content/usermanagement/userRole/menu/list?FatherControllerName="+$scope.controllerName+"&fk_client="+$scope.fk_client+"&fk_roleid="+$scope.form.fk_roleid+"&type="+$scope.type+"&fk_groupid="+$scope.fk_groupid};
					eventBusService.publish("modules.content.load",menu)
				}else{
					alert(data.msg);
				}
			});
		}

		$scope.deleteApp = function(){
			httpService.post(app.urls.role_power_delete,{"fk_roleid":$scope.form.fk_roleid,"pk_app":$scope.form.pk_app,"type":"1"},function(data){
				if("0000" === data.code){
					$scope.saveApp();
				}else{
					alert(data.msg);
				}
			});
		}

		//选中
		$scope.checkone = function(fk_client,$event) {
			$scope.form.pk_app = fk_client;
			if ($($event.target)[0].checked) {
				httpService.post(app.urls.role_power_insert,{"fk_roleid":$scope.form.fk_roleid,"pk_app":$scope.form.pk_app,"type":"1"},function(data){
					if("0000" === data.code){
					}else{
						alert(data.msg);
					}
				});
			}else {
				httpService.post(app.urls.role_power_delete,{"fk_roleid":$scope.form.fk_roleid,"pk_app":$scope.form.pk_app,"type":"1"},function(data){
					if("0000" === data.code){
					}else{
						alert(data.msg);
					}
				});
			}
		}
	}}});